//
//  UserActionVC.swift
//  com.yunshu.yunshuapp
//
//  Created by 周文杰 on 2020/8/11.
//  Copyright © 2020 zwj. All rights reserved.
//

import UIKit

class UserActionVC: UIViewController {

    static let VCID = "UserActionVC"

    @IBOutlet weak var userInfo: UIView!
    
    @IBOutlet weak var logout: UIView!
    
    @IBOutlet weak var unregister: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "个人中心"
        self.view.isUserInteractionEnabled = true
        
        self.userInfo.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(UserActionVC.enterUserInfo)))
        
        self.logout.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(UserActionVC.logout2)))
        
        self.unregister.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(UserActionVC.unregister2)))
        }
    
    @objc func enterUserInfo(){
        let vc:ModifyUserVC = ModifyUserVC.init()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc func logout2(){
        let alert = UIAlertController(title: "提示", message:"确定要退出登录吗？", preferredStyle: UIAlertController.Style.alert)
        alert.addAction(UIAlertAction(title: "点错了", style: UIAlertAction.Style.default, handler: nil))
        alert.addAction(UIAlertAction(title: "确定", style: UIAlertAction.Style.cancel, handler: {
            params in
                User.logout()
                self.navigationController?.popViewController(animated: true)
            }))
        self.navigationController?.present(alert, animated: true, completion: nil)
        
    }
    
    @objc func unregister2(){
        let alert = UIAlertController(title: "提示", message:"确定要注销账号吗？您将失去您再云数智惠上的所有数据！", preferredStyle: UIAlertController.Style.alert)
        alert.addAction(UIAlertAction(title: "点错了", style: UIAlertAction.Style.default, handler: nil))
        alert.addAction(UIAlertAction(title: "确定注销", style: UIAlertAction.Style.cancel, handler: {
            params in
                self.httpUnregister()
            }))
        self.navigationController?.present(alert, animated: true, completion: nil)
        
    }
    
    func httpUnregister() {
                
        let params = ["id": User.get.userId]
        _ = NetworkManager.GET(NetworkManager.UNREGISTER,params:params) { (isSuccess, resp) in
            if isSuccess {
                User.logout()
                let alert = UIAlertController(title: "提示", message:"注销成功！", preferredStyle: UIAlertController.Style.alert)
                let action = UIAlertAction.init(title: "我知道了", style: .cancel, handler: { (action1) in
                    self.navigationController?.popViewController(animated: true)
                })
                alert.addAction(action)
                self.navigationController?.present(alert, animated: true, completion: nil)
            }else {
                let alert = UIAlertController(title: "提示", message:"注销失败，请稍后重试！", preferredStyle: UIAlertController.Style.alert)
                let action = UIAlertAction.init(title: "我知道了", style: .cancel, handler: { (action1) in
                })
                alert.addAction(action)
                self.navigationController?.present(alert, animated: true, completion: nil)
            }
        }
        
    }
}
